package com.dragon.mmochat.message;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapPoint;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_4_11S extends SkymobiProtocolMessage {
	private byte actionType;// 0:增加；1:修改
	private MmochatTask task;
	private MmochatPlayer me;

	public Mmochat_4_11S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_4_11);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		// 闪动键和闪动提示语
		if (task.getHintKey() == null) {
			out.put((byte) 0);
			out.putInt(0);
		} else {
			out.put((byte) 1);
			out.putInt(task.getHintKey());
		}
		if (task.getHintWord() == null) {
			out.put((byte) 0);
		} else {
			out.putPrefixedString(task.getHintWord(), 1);
		}

		// 变更类型
		out.put(actionType);
		// 任务ID
		out.putInt(task.getTaskId());
		// 任务名
		out.putPrefixedString(task.getTaskName(), 1);
		// 任务进度
		if (task.getTaskType() != MmochatTaskType.过渡任务
				&& task.getTaskType() != MmochatTaskType.寻宝任务
				&& task.getFinish_need_battles() == null
				&& me.canFinishTask(task.getTaskId())) {
			out.putPrefixedString("可交付", 1);
		} else {
			out.putPrefixedString("进行中", 1);
		}

		// 交任务NPC名字
		MmochatNpc npc = MmochatNpcService.getNpcById(task.getFinish_npcId());
		if (npc != null) {
			out.putPrefixedString(npc.getName(), 1);
		} else {
			out.putPrefixedString("-", 1);
		}

		// 任务寻路信息
		MmochatMapPoint guider = task.getGuiderPoint(me);
		if (guider != null) {
			out.putInt(guider.getMapId());
			out.putShort((short) guider.getX());
			out.putShort((short) guider.getY());
		} else {
			out.putInt(-1);
			out.putShort((short) 0);
			out.putShort((short) 0);
		}
	}

	public void setActionType(int actionType) {
		this.actionType = (byte) actionType;
	}

	public void setTask(MmochatTask task) {
		this.task = task;
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

}
